package com.bodacn.keypoint;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import org.json.JSONArray;
import org.json.JSONException;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentScan extends Fragment implements View.OnClickListener {
    public static String TAG= FragmentScan.class.getName();
    public static final int msgFocusBarcode = 1001;
    public static final int MSG_SCANBARCODE = 9001;
    public static final int MSG_EMPLIST=9002;
    public static final int MSG_PROCLIST =9003;
    //用于将Activity中的参数传给Fragment
    private Map<String,Object> fragmentParams=new HashMap<String,Object>();
    ReentrantLock reentrantLock = new ReentrantLock();
    //用于与主Activity通讯;
    private Handler mainHandler;
    //全局变量
    Toast curToast;
    String curLoginUserNo,curLoginUser,curServiceUrl="",curServiceDir="",curDepartNo,curDepart,curFactory,CPUID;
    Button btn_find,btn_Query,btn_ClearEmp;
    ImageButton btn_editBarcode;
    ListView detailListView,empListView;
    TextView txt_HintInfo,txt_OrderNo,txt_StyleNo,txt_PO,txt_Color,txt_PackNo,txt_Size,txt_StyleSort,txt_PartName,txt_Number,txt_TotalNum;
    EditText edt_barcode;
    JSONArray resultJSON, empJSON, procJSON;
    String KeyGuid= UUID.randomUUID().toString().toUpperCase();

    //主程序回调传参--------------------------------------------开始;
    private FragmentScanCallBack mFragmentScanCallBack;
    /*定义一个业务接口,主程序负责实现，所以需在主程序implements中加上这个接口的引用>>>>>切记<<<<<<
    该Fragment所在Activity需要实现该接口
    该Fragment将通过此接口与它所在的Activity交互*/
    public interface FragmentScanCallBack {
        // 里面传个值@param Map<String,Object>适应多种类型;
        void FragmentScanWorkAndTransParams(Map<String, Object> Params);
    }

    //主程序回调传参--------------------------------------------结束;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Activity activity = (Activity) context;
        if (!(activity instanceof FragmentScanCallBack)) {
            throw new IllegalStateException("MainActivity必须实现FragmentScanCallBack接口");
        }
        mFragmentScanCallBack=(FragmentScanCallBack) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mFragmentScanCallBack=null;
    }

    //判断是否为数字，不然不转
    public static boolean isNumeric(String str){
        try {
            new BigDecimal(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public FragmentScan() {
        // Required empty public constructor

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //得到Activity的Handler,用于发消息给主Activity;
        MainActivity activity=(MainActivity) getActivity();
        mainHandler=activity.mainHandler;
        //加接口监听
        activity.setTransParamsListenerScan(new MainActivity.OnTransParamsListenerScanNC() {
            @Override
            public void TransParams(Map<String, Object> mParams) {
                if (mParams!=null) {
                    try {
                        String paramComm= mParams.get("ParamComm").toString();
                        if (paramComm=="LoginParam")
                        {
                            Log.d(this.getClass().getName(),"LoginParam------------------>1");
                            //传递登陆参数信息;
                            curLoginUserNo=mParams.get("LoginUserNo").toString();
                            curLoginUser=mParams.get("LoginUser").toString();
                            curServiceUrl=mParams.get("ServiceUrl").toString();
                            curServiceDir= mParams.get("ServiceDir").toString();
                            curDepartNo=mParams.get("DepartNo").toString();
                            curDepart=mParams.get("Depart").toString();
                            curFactory=mParams.get("Factory").toString();
                            CPUID=mParams.get("CPUID").toString();
                            Log.d(this.getClass().getName(),"LoginParam------------------>2");
                            KeyGuid= UUID.randomUUID().toString().toUpperCase();
                            //发消息置焦点
                            Message msg =new Message();
                            msg.what = msgFocusBarcode;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        }
                        if ((paramComm=="Barcode")){
                            String barcode=mParams.get("Barcode").toString();
                            edt_barcode.setText(barcode);
                            searchBarcodeData(barcode,txt_OrderNo.getText().toString(),txt_StyleNo.getText().toString());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }






    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏状态栏和底部虚拟按键
        View decorView = getActivity().getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int flag = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(flag);
        }
    }
    //设置焦点到扫描输入框
    private void setEditBarcodeFocus()
    {
        //传消息给主界面;
        if (mFragmentScanCallBack!=null)
        {
            fragmentParams.clear();
            fragmentParams.put("ParamComm","AllowScan");//命令;
            fragmentParams.put("ParamValue","Y");//参数;
            mFragmentScanCallBack.FragmentScanWorkAndTransParams(fragmentParams);
        } else Log.d("msg","mFragmentECallBack-----------空的咋办");
        //设置焦点
        System.out.println(this.getClass().getName()+" 设置焦点-------------------------------1.Start");
        if (edt_barcode!=null) {
            edt_barcode.selectAll();
            edt_barcode.setFocusable(true);
            edt_barcode.setFocusableInTouchMode(true);
            edt_barcode.requestFocus();
            edt_barcode.requestFocusFromTouch();
            hideIM(getView());
            hideBottomUIMenu();
        }
        System.out.println(this.getClass().getName()+" 设置焦点-------------------------------1.End");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_scan, container, false);
        InitView(root);
        System.out.println(this.getClass().getName()+"_onCreateView");
        return root;
    }
    //初始化;
    public void InitView(View view){
        empListView=view.findViewById(R.id.empListView);
        empListView.setFocusable(false);
        empListView.setFocusableInTouchMode(false);
        detailListView=(ListView) view.findViewById(R.id.detailListView);
        detailListView.setFocusable(false);
        detailListView.setFocusableInTouchMode(false);
        btn_ClearEmp=(Button)view.findViewById(R.id.btn_ClearEmp);
        btn_ClearEmp.setOnClickListener(this);
        view.findViewById(R.id.btn_ClearProc).setOnClickListener(this);
        btn_find=(Button)view.findViewById(R.id.btn_find);
        btn_find.setOnClickListener(this);
        btn_Query=(Button)view.findViewById(R.id.btn_Query);
        btn_Query.setOnClickListener(this);
        btn_editBarcode=(ImageButton)view.findViewById(R.id.btn_editBarcode);
        btn_editBarcode.setOnClickListener(this);
        btn_editBarcode.setFocusable(false);
        btn_editBarcode.setFocusableInTouchMode(false);
        //界面Edit;
        edt_barcode=(EditText)view.findViewById(R.id.edt_barcode);
        edt_barcode.setInputType(InputType.TYPE_NULL);
        edt_barcode.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event!=null){
                    if ((event.getAction()==KeyEvent.ACTION_DOWN)&&(event.getKeyCode()== KeyEvent.KEYCODE_ENTER))
                    {
                        //虚拟键盘或实体键盘可考虑是否通过deviceID，或source来判断
                        // KeyEvent:KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ENTER, scanCode=0, metaState=0, flags=0x16, repeatCount=0, eventTime=142515176, downTime=142515176, deviceId=-1, source=0x0 }
                        //隐藏键盘
                        hideIM(v);
                        if (edt_barcode.getText().length()==0) {
                            curToast.setText(getString(R.string.PleaseInputBarcode));
                            curToast.show();
                        } else {
                            searchBarcodeData(edt_barcode.getText().toString(),txt_OrderNo.getText().toString(), txt_StyleNo.getText().toString());
                        }
                        //未有焦点
                        if (edt_barcode.isFocused()==false){
                            //发消息置焦点
                            Message msg =new Message();
                            msg.what = msgFocusBarcode;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        } else edt_barcode.selectAll();
                    }

                }
                return false;
            }
        });
        edt_barcode.requestFocus();
        edt_barcode.requestFocusFromTouch();
        txt_HintInfo=(TextView)view.findViewById(R.id.txt_HintInfo);
        txt_OrderNo=(TextView)view.findViewById(R.id.txt_OrderNo);
        txt_StyleNo=(TextView)view.findViewById(R.id.txt_StyleNo);
        txt_PO=(TextView)view.findViewById(R.id.txt_PO);
        txt_Color=(TextView)view.findViewById(R.id.txt_Color);
        txt_PackNo=(TextView)view.findViewById(R.id.txt_PackNo);
        txt_Size=(TextView)view.findViewById(R.id.txt_Size);
        txt_Number=(TextView)view.findViewById(R.id.txt_Number);
        txt_TotalNum=(TextView)view.findViewById(R.id.txt_TotalNum);
        txt_StyleSort=(TextView)view.findViewById(R.id.txt_StyleSort);
        txt_PartName=(TextView)view.findViewById(R.id.txt_PartName);

        //初始提醒;
        curToast = Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT);

    }


    //插放音乐;
    private void playRingtone(){
        Uri notification = Uri.parse("android.resource://"+getActivity().getPackageName()+"/"+ R.raw.scan);
        Ringtone r = RingtoneManager.getRingtone(getActivity(), notification);
        r.play();

    }
    //刷新缸布列表;
    private void searchBarcodeData(String BarcodeParam,String OrderNoParam,String StyleNoParam)
    {
        String SqlComm="Exec KeyPoint_WorkScan @Barcode='" + BarcodeParam + "',@OrderNo='" + OrderNoParam + "',@StyleNo='" + StyleNoParam
                + "',@OPERTYPE='SCANBARCODE',@UserNo='"+curLoginUserNo+ "',@KeyGuid='"+KeyGuid+"',@TermNo='"+MainApplication.TermNo+"'";
        //调用WebService过程并处理界面更新;
        ReadSQLData(SqlComm);
    }

    // 隐藏手机键盘
    private void hideIM(View edt) {
        try {
            InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
            IBinder windowToken = edt.getWindowToken();
            if (windowToken != null) {
                im.hideSoftInputFromWindow(windowToken, 0);
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onClick(View v) {
        hideIM(v);
        switch (v.getId()){
            case R.id.btn_find:
                if (edt_barcode.getText().length()==0) {
                    curToast.setText(getString(R.string.PleaseInputBarcode));
                    curToast.show();
                } else //执行查询;
                {
                    searchBarcodeData(edt_barcode.getText().toString(), txt_OrderNo.getText().toString(), txt_StyleNo.getText().toString());
                }
                break;
            case R.id.btn_ClearEmp:
                TransEmpList("CLEAREMP");
                hideIM(v);
                break;
            case R.id.btn_ClearProc:
                TransProcList("CLEARPROC");
                hideIM(v);
                break;
            case R.id.btn_Query:
                Intent tmpQueryIntent=new Intent(getContext(),QueryActivity.class);
                Bundle bundle=new Bundle();
                bundle.clear();
                bundle.putString("LoginUserNo", curLoginUserNo);
                bundle.putString("LoginUser",curLoginUser);
                bundle.putString("CurDepartNo",curDepartNo);
                bundle.putString("CurDepart",curDepart);
                bundle.putString("CurFactory",curFactory);
                bundle.putString("ServiceUrl",curServiceUrl);
                bundle.putString("ServiceDir",curServiceDir);
                bundle.putString("CPUID",CPUID);
                bundle.putString("QueryTitle", "计件扫描查询");
                bundle.putString("OrderNo",txt_OrderNo.getText().toString());
                bundle.putString("StyleNo",txt_StyleNo.getText().toString());
                bundle.putString("StyleSort",txt_StyleSort.getText().toString());
                bundle.putString("NeedOrder","NO");
                tmpQueryIntent.putExtras(bundle);
                startActivity(tmpQueryIntent);
                break;
            case R.id.btn_editBarcode:
                edt_barcode.setInputType(InputType.TYPE_CLASS_TEXT);
                //1.得到InputMethodManager对象
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
                //2.调用showSoftInput方法显示软键盘，其中view为聚焦的view组件
                imm.showSoftInput(edt_barcode,InputMethodManager.SHOW_FORCED);
                break;

            default:
                break;
        }
    }

    private void TransEmpList(String paramOperType) {
        try {
            empJSON=new JSONArray("[]");
            EmpListAdapter empListAdapter = new EmpListAdapter(getActivity());
            empListView.setAdapter(empListAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //执行SQL过程
        String SqlComm="Exec KeyPoint_WorkScan @OPERTYPE='"+paramOperType.toUpperCase()+"',@UserNo='"+curLoginUserNo+ "',@KeyGuid='"+KeyGuid+ "',@TermNo='"+MainApplication.TermNo+"'";
        //调用WebService过程并处理界面更新;
        ReadSQLData(SqlComm);
    }
    private void TransProcList(String paramOperType) {
        try {
            procJSON=new JSONArray("[]");
            DetailListAdapter detailListAdapter = new DetailListAdapter(getActivity());
            detailListView.setAdapter(detailListAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //执行SQL过程
        String SqlComm="Exec KeyPoint_WorkScan @OPERTYPE='"+paramOperType.toUpperCase()+"',@UserNo='"+curLoginUserNo+ "',@KeyGuid='"+KeyGuid+ "',@TermNo='"+MainApplication.TermNo+"'";
        //调用WebService过程并处理界面更新;
        ReadSQLData(SqlComm);
    }

    /**
     * 定义一个Handler用于接收Fragment发出来的指令
     */
    public Handler fragmentHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg!=null){
                //任意消息来均置焦点
                setEditBarcodeFocus();
                System.out.println("收到的消息是："+msg.what);
                curToast.setText("收到的消息是："+msg.what);
                curToast.show();
                int msgId = msg.what;
                switch (msgId) {
                    case MSG_SCANBARCODE:
                        fillFormtext();
                        break;
                    case MSG_EMPLIST:
                        if ((empJSON!=null)&&(empJSON.length()>0)) {
                            try {
                                if (empJSON.optJSONObject(0).has("KeyGuid")) KeyGuid=empJSON.optJSONObject(0).getString("KeyGuid");
                                if (empJSON.optJSONObject(0).has("HaveData")){
                                    if (empJSON.optJSONObject(0).getString("HaveData").toUpperCase().equals("Y"))
                                    {
                                        EmpListAdapter empListAdapter = new EmpListAdapter(getActivity());
                                        empListView.setAdapter(empListAdapter);
                                    }
                                } else{
                                    if (empJSON.optJSONObject(0).has("HintInfo") == true)
                                        txt_HintInfo.setText(empJSON.getJSONObject(0).getString("HintInfo"));
                                    if (empJSON.optJSONObject(0).has("ErrInfo") == true) {
                                        AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                                                .setTitle("系统提示").setMessage(empJSON.getJSONObject(0).getString("ErrInfo"))
                                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        return;
                                                    }
                                                }).create(); // 创建对话框
                                        alertDialog.show(); // 显示对话框
                                    }

                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        break;
                    case MSG_PROCLIST:
                        if ((procJSON!=null)&&(procJSON.length()>0)) {
                            try {
                                if (procJSON.optJSONObject(0).has("KeyGuid")) KeyGuid=procJSON.optJSONObject(0).getString("KeyGuid");
                                if (procJSON.optJSONObject(0).has("HaveData")){
                                    if (procJSON.optJSONObject(0).getString("HaveData").toUpperCase().equals("Y"))
                                    {
                                        DetailListAdapter detailListAdapter = new DetailListAdapter(getActivity());
                                        detailListView.setAdapter(detailListAdapter);
                                    }
                                } else{

                                    if (procJSON.optJSONObject(0).has("HintInfo") == true)
                                        txt_HintInfo.setText(procJSON.getJSONObject(0).getString("HintInfo"));
                                    if (procJSON.optJSONObject(0).has("ErrInfo") == true) {
                                        AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                                                .setTitle("系统提示").setMessage(procJSON.getJSONObject(0).getString("ErrInfo"))
                                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        return;
                                                    }
                                                }).create(); // 创建对话框
                                        alertDialog.show(); // 显示对话框
                                    }

                                }
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    };


    //直接连获取数据，与Handler配套使用;
    private void ReadSQLData(final String SQLComm) {
        System.out.println("SQLComm:"+SQLComm);
        Runnable run = new Runnable() {
            @Override
            public synchronized void run() {
                try{
                    reentrantLock.lock();
                    JSONArray jsonArray  = DBUtil.QuerySQL2JSON(SQLComm);
                    //往界面转数据;
                    if ((jsonArray!=null)&&(jsonArray.length()>0)) {
                        String OperType="";
                        if (jsonArray.optJSONObject(0).has("OperType")==true)  OperType=jsonArray.optJSONObject(0).getString("OperType");
                        if (jsonArray.optJSONObject(0).has("OPERTYPE")==true)  OperType=jsonArray.optJSONObject(0).getString("OPERTYPE");
                        if (OperType.toUpperCase().equals("SCANBARCODE")) {
                            resultJSON=jsonArray;
                            //刷新界面;
                            Message msg = new Message();
                            msg.what = MSG_SCANBARCODE;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                            return;
                        }
                        if (OperType.toUpperCase().equals("EMPLIST")||OperType.toUpperCase().equals("CLEAREMP")||OperType.toUpperCase().equals("GETEMP"))
                        {
                            empJSON=jsonArray;
                            //刷新界面;
                            Message msg = new Message();
                            msg.what = MSG_EMPLIST;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                            return;
                        }
                        if (OperType.toUpperCase().equals("PROCLIST")||OperType.toUpperCase().equals("CLEARPROC")||OperType.toUpperCase().equals("GETPROC"))
                        {
                            procJSON =jsonArray;
                            //刷新界面;
                            Message msg = new Message();
                            msg.what = MSG_PROCLIST;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                            return;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    reentrantLock.unlock();
                }finally {
                    reentrantLock.unlock();
                }

            }

        };
        new Thread(run).start();
    }




    //订单表格适配器;
    public final class DetailListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public DetailListAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return procJSON.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return procJSON.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) convertView = inflater.inflate(R.layout.fragment_scan_procitem, null, false);
            int pullVatListWidth=parent.getWidth();
            convertView.setLayoutParams(new ListView.LayoutParams((int)pullVatListWidth,(int)(38)));
            int color = getResources().getColor(R.color.text_black);
            String tmpValue;
            try {
                Log.d("msg","getView()----------------------------------------------------->"+position);
                if (procJSON.optJSONObject(position).has("ConColor")==true) {
                    tmpValue = procJSON.optJSONObject(position).getString("ConColor").toString();
                    if (tmpValue.equals("1"))
                        color = getResources().getColor(R.color.text_blue);
                    if (tmpValue.equals("2"))
                        color = getResources().getColor(R.color.text_red);
                    if (tmpValue.equals("3"))
                        color = getResources().getColor(R.color.text_gray);
                    if (tmpValue.equals("0"))
                        color = getResources().getColor(R.color.text_black);
                }
                TextView tv = (TextView) convertView.findViewById(R.id.txt_item_WorkNo);// 显示文字
                if (procJSON.optJSONObject(position).has("WorkNo")==true)
                    tv.setText(procJSON.optJSONObject(position).get("WorkNo").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_WorkName);// 显示文字
                if (procJSON.optJSONObject(position).has("WorkName")==true)
                    tv.setText(procJSON.optJSONObject(position).get("WorkName").toString());
                tv.setTextColor(color);

            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
            Log.d("msg","DetailGridAdapter_position:"+position);
            return convertView;
        }
    }


    //订单表格适配器;
    public final class EmpListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public EmpListAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return empJSON.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return empJSON.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) convertView = inflater.inflate(R.layout.fragment_scan_empitem, null, false);
            int pullVatListWidth=parent.getWidth();
            convertView.setLayoutParams(new ListView.LayoutParams((int)pullVatListWidth,(int)(38)));
            int color = getResources().getColor(R.color.text_black);
            String tmpValue;
            try {
                Log.d("msg","getView()----------------------------------------------------->"+position);
                if (empJSON.optJSONObject(position).has("ConColor")==true) {
                    tmpValue = empJSON.optJSONObject(position).getString("ConColor").toString();
                    if (tmpValue.equals("1"))
                        color = getResources().getColor(R.color.text_blue);
                    if (tmpValue.equals("2"))
                        color = getResources().getColor(R.color.text_red);
                    if (tmpValue.equals("3"))
                        color = getResources().getColor(R.color.text_gray);
                    if (tmpValue.equals("0"))
                        color = getResources().getColor(R.color.text_black);
                }
                TextView tv = (TextView) convertView.findViewById(R.id.txt_item_EmpNo);// 显示文字
                if (empJSON.optJSONObject(position).has("EmpNo")==true)
                    tv.setText(empJSON.optJSONObject(position).get("EmpNo").toString());
                tv.setTextColor(color);
                tv = (TextView) convertView.findViewById(R.id.txt_item_EmpName);// 显示文字
                if (empJSON.optJSONObject(position).has("EmpName")==true)
                    tv.setText(empJSON.optJSONObject(position).get("EmpName").toString());
                tv.setTextColor(color);
            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
            Log.d("msg","EmpListAdapter:"+position);
            return convertView;
        }
    }

    //清除界面显示内容
    private void clearFormText(){
        txt_HintInfo.setText("");
        txt_OrderNo.setText("");
        txt_StyleNo.setText("");
        txt_PO.setText("");
        txt_Color.setText("");
        txt_PackNo.setText("");
        txt_Size.setText("");
        txt_StyleSort.setText("");
        txt_PartName.setText("");
        txt_Number.setText("");
        txt_HintInfo.setText("");
    }

    //填充界面显示内容;
    private void fillFormtext(){
        if (resultJSON.length()>0)
        {
            try {
                if (resultJSON.getJSONObject(0).has("OrderNo")==true) txt_OrderNo.setText(resultJSON.getJSONObject(0).getString("OrderNo"));
                if (resultJSON.getJSONObject(0).has("StyleNo")==true) txt_StyleNo.setText(resultJSON.getJSONObject(0).getString("StyleNo"));
                if (resultJSON.getJSONObject(0).has("PO")==true) txt_PO.setText(resultJSON.getJSONObject(0).getString("PO"));
                if (resultJSON.getJSONObject(0).has("Color")==true) txt_Color.setText(resultJSON.getJSONObject(0).getString("Color"));
                if (resultJSON.getJSONObject(0).has("PackNo")==true) txt_PackNo.setText(resultJSON.getJSONObject(0).getString("PackNo"));
                if (resultJSON.getJSONObject(0).has("Size")==true) txt_Size.setText(resultJSON.getJSONObject(0).getString("Size"));
                if (resultJSON.getJSONObject(0).has("StyleSort")==true) txt_StyleSort.setText(resultJSON.getJSONObject(0).getString("StyleSort"));
                if (resultJSON.getJSONObject(0).has("PartName")==true) txt_PartName.setText(resultJSON.getJSONObject(0).getString("PartName"));

                if (resultJSON.getJSONObject(0).has("Number")==true) txt_Number.setText(resultJSON.getJSONObject(0).getString("Number"));
                if (resultJSON.getJSONObject(0).has("TotalNum")==true) txt_TotalNum.setText(resultJSON.getJSONObject(0).getString("TotalNum"));

                if (resultJSON.getJSONObject(0).has("HintInfo")==true) txt_HintInfo.setText(resultJSON.getJSONObject(0).getString("HintInfo"));
                if (resultJSON.getJSONObject(0).has("ErrInfo")==true)
                {
                    AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                            .setTitle("系统提示").setMessage(resultJSON.getJSONObject(0).getString("ErrInfo"))
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    return;
                                }
                            }).create(); // 创建对话框
                    alertDialog.show(); // 显示对话框
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


    }

    @Override
    public void onStart() {
        super.onStart();
        //设置焦点
        System.out.println(this.getClass().getName()+" 设置焦点-------------------------------1.Start");
        setEditBarcodeFocus();
        System.out.println(this.getClass().getName()+" 设置焦点-------------------------------1.End");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden==false){
            TransEmpList("GETEMP");
            TransProcList("GETPROC");
        }
    }
}
